/*
 *  $Id: WoodenTower.cs 1063 2010-05-20 17:40:43Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008-2009 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;

namespace Ragima.Cards001.Earth.Abilities {
    public class WoodenTower : AbilityCard<Creature> {
        public WoodenTower() : base("001_e_a_wooden_tower") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Earth;
            Target = new CreatureTarget(GetTargetPhrase(), true, false, IsTarget);
            AddCondition(new AffectCondition<WoodenTowerAffect>(GetConditionPhrase(), false));
        }

        private static bool IsTarget(TargetRequest request, Creature target) {
            return request.Caster != target && !target.Disabled;
        }

        protected override void DoAction(TargetArgs<Creature> args) {
            Creature troll = args.Caster as Creature;
            if (troll == null)
                return;
            troll.AddAffect(new WoodenTowerAffect(args.Target));
            troll.State = CreatureState.Tapped;
            args.Target.SetNoneCardReturn(true);
            args.Target.InstantDeath();
        }

        private class WoodenTowerAffect : SpellAffect, ICollector<AbilityCard> {
            private Creature _creature;
            private int _health;

            public WoodenTowerAffect(Creature creature) : base(true) {
                DefaultDescription = true;
                _creature = creature;
                Add(new Affects.AbilityTireless());
                foreach (Affect affect in creature.GetAffects(false))
                    if (!(affect is NaturalAffect))
                        Add(affect.Clone());
                creature.Card.CollectAbilities(this);
                _health = creature.Health;
            }

            public override string Id {
                get { return AffectManager.GetCardId<WoodenTower>(null); }
            }

            public override Card BaseCard {
                get { return _creature.Card; }
            }

            public override string DescriptionText {
                get { return Description.GetText(Owner, _creature, _health); }
            }

            public override bool Dispellable {
                get { return false; }
            }

            public override bool IsEnded {
                get { return _health <= 0; }
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnBeforeDamage += DoDamage;
                LocalSummary.OnUseAbility += DoAbility;
            }

            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnBeforeDamage -= DoDamage;
                LocalSummary.OnUseAbility -= DoAbility;
            }

            private void DoDamage(AbstractObject target, DamageInfo damage) {
                int dmg = Math.Min(damage.RealDamage / 2, _health);
                if (dmg > 0) {
                    _health -= dmg;
                    damage.RealDamage -= dmg;
                }
            }

            private void DoAbility(AbilityCard ability, TargetArgs args) {
                foreach (AbilityCard ab in args.Caster.GetAbilities())
                    if (!(ab is WoodenTower))
                        args.Caster.SetUsedAbility(ab);
            }

            public override void OnRemove() {
                _creature.SetNoneCardReturn(false);
                _creature.MoveCardAfterDeath();
            }

            private void AddAbility(AbilityCard ability) {
                Add(new Affects.AbilityAffect(ability));
            }

            void ICollector<AbilityCard>.Add(AbilityCard item) {
                AddAbility(item);
            }

            void ICollector<AbilityCard>.Add<D>() {
                AddAbility(new D());
            }
        }
    }
}
